package com.cy.aircompressor.main.home.fragment.provider;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.TimeUtils;
import com.cy.aircompressor.R;
import com.cy.aircompressor.databinding.ItemMaintenanceRecordBinding;
import com.cy.aircompressor.entity.maintenance.MaintenanceRecord;
import com.drug.lib.view.recycptr.ItemViewBinder;
import com.net.model.base.BaseBindViewHolder;

public class MaintenanceRecordProvider extends ItemViewBinder<MaintenanceRecord, MaintenanceRecordProvider.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new ViewHolder(ItemMaintenanceRecordBinding.inflate(inflater, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull MaintenanceRecord entity) {
        holder.mBinding.airName.setText(entity.getCompressorName());
        holder.mBinding.nextTime.setText(entity.getNextMaintenanceDate());

        long nextTime = TimeUtils.string2Millis(entity.getNextMaintenanceDate(), "yyyy-MM-dd");
        long health = nextTime - System.currentTimeMillis();
        if (health > 0) {
            int dayCount = (int) Math.ceil(health / (1000 * 60 * 60 * 24));
            holder.mBinding.fromToday.setText(dayCount + "天");
            if (dayCount >= 350) {
                holder.mBinding.fromToday.setTextColor(Color.parseColor("#40ED0B"));
            } else if (dayCount < 100) {
                holder.mBinding.fromToday.setTextColor(Color.parseColor("#EFAE30"));
            } else if (dayCount < 20) {
                holder.mBinding.fromToday.setTextColor(Color.parseColor("#F04538"));
            }
        } else {
            holder.mBinding.fromToday.setText("维保时间已过");
        }
        holder.itemView.setBackground(ContextCompat.getDrawable(holder.itemView.getContext(), (entity.isSelect() ?
                R.drawable.backgroud_select_status_draw : R.drawable.backgroud_deep_shallow_draw)));
    }

    public class ViewHolder extends BaseBindViewHolder<ItemMaintenanceRecordBinding> {

        protected ViewHolder(ItemMaintenanceRecordBinding itemView) {
            super(itemView);
        }
    }
}
